from fastapi import APIRouter, Body, Depends
from sqlalchemy.orm import Session


from database import get_session, transactional_session
from mldong.auth_middleware import SaCheckPermission, SaMode
from mldong.base import R, CommonPage, CommonResult, IdParam, IdsParam
from modules.sys.params.file_info_param import FileInfoPageParam, FileInfoParam
from modules.sys.services.file_info_service import FileInfoService
from modules.sys.vos.file_info_vo import FileInfoVO

tags = ["文件信息"]
router = APIRouter(tags=tags)


def get_file_info_service(db: Session = Depends(get_session)) -> FileInfoService:
    """
    获取文件信息服务实例的依赖函数
    """
    return FileInfoService(db)


@router.post("/sys/fileInfo/save", summary="添加文件信息", response_model=CommonResult, response_model_exclude_none=True)
@SaCheckPermission("sys:fileInfo:save")
async def file_info_save(data: FileInfoParam = Body(description="文件信息参数"), file_info_service: FileInfoService = Depends(get_file_info_service)):
    with transactional_session(file_info_service.db):
        file_info_service.save(data)
    return R.success()


@router.post("/sys/fileInfo/update", summary="修改文件信息", response_model=CommonResult, response_model_exclude_none=True)
@SaCheckPermission("sys:fileInfo:update")
async def file_info_update(data: FileInfoParam = Body(description="文件信息参数"), file_info_service: FileInfoService = Depends(get_file_info_service)):
    with transactional_session(file_info_service.db):
        file_info_service.update(data)
    return R.success()


@router.post("/sys/fileInfo/remove", summary="删除文件信息", response_model=CommonResult, response_model_exclude_none=True)
@SaCheckPermission("sys:fileInfo:remove")
async def file_info_remove(param: IdsParam = Body(), file_info_service: FileInfoService = Depends(get_file_info_service)):
    with transactional_session(file_info_service.db):
        file_info_service.remove_by_ids(param.ids)
    return R.success()


@router.post("/sys/fileInfo/detail", summary="文件信息详情", response_model=CommonResult[FileInfoVO], response_model_exclude_none=True)
@SaCheckPermission(["sys:fileInfo:detail", "sys:fileInfo:update"], mode=SaMode.OR)
async def file_info_detail(param: IdParam = Body(), file_info_service: FileInfoService = Depends(get_file_info_service)):
    data = file_info_service.detail(param.id)
    return R.data(data)


@router.post("/sys/fileInfo/page", summary="分页查询文件信息列表", response_model=CommonResult[CommonPage[FileInfoVO]], response_model_exclude_none=True)
@SaCheckPermission("sys:fileInfo:page")
async def file_info_page(param: FileInfoPageParam = Body(), file_info_service: FileInfoService = Depends(get_file_info_service)):
    data = file_info_service.page(param)
    return R.data(data)
